package com.zh;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Update {
	private SessionFactory sFactory;
	private Util util;
	
	
	public SessionFactory getsFactory() {
		return sFactory;
	}
	public void setsFactory(SessionFactory sFactory) {
		this.sFactory = sFactory;
	}
	public Util getUtil() {
		return util;
	}
	public void setUtil(Util util) {
		this.util = util;
	}
	
	public void updateTable(String tablename, Object object,String id){
		Session session=sFactory.openSession();
		Transaction transaction=session.beginTransaction();
		if(tablename.equals("level")){
			Level le = (Level)session.get(Level.class,id);
			Level content = (Level)object;
			le.setLpwd(content.getLpwd());
			le.setLlevel(content.getLlevel());
			session.save(le);
		}
		else if(tablename.equals("user")){
			User userinfo=(User)session.get(User.class, id);
			userinfo.setUserId(((User)object).getUserId());
			userinfo.setHouseId(((User)object).getHouseId());
			userinfo.setUsername(((User)object).getUsername());
			userinfo.setUsersex(((User)object).getUsersex());
			userinfo.setUserprofession(((User)object).getUserprofession());
			userinfo.setPhonenumber(((User)object).getPhonenumber());
			userinfo.setLivetime(((User)object).getLivetime());
			userinfo.setLivecount(((User)object).getLivecount());
			session.save(userinfo);
		}
		else if(tablename.equals("house")){
			House house=(House)session.get(House.class, id);
			house.setHouseId(((House)object).getHouseId());
			house.setHouseaddress(((House)object).getHouseaddress());
			house.setHousesquare(((House)object).getHousesquare());
			session.save(house);
		}
		else if(tablename.equals("payment")){
			Payment program=(Payment)session.get(Payment.class, id);
			program.setPaymentId(((Payment)object).getPaymentId());
			program.setUserId(((Payment)object).getUserId());
			program.setGasmoney(((Payment)object).getGasmoney());
			program.setStopcarmoney(((Payment)object).getStopcarmoney());
			program.setWatermoney(((Payment)object).getWatermoney());
			program.setElementmoney(((Payment)object).getElementmoney());
			program.setServicemoney(((Payment)object).getServicemoney());
			program.setStarttime(((Payment)object).getStarttime());
			program.setEndtime(((Payment)object).getEndtime());
			program.setStates(((Payment)object).getStates());
			session.save(program);
		}
		else if(tablename.equals("repair")){
			Repair repair=(Repair)session.get(Repair.class, id);
			repair.setRepairId(((Repair)object).getRepairId());
			repair.setRequesttime(((Repair)object).getRequesttime());
			repair.setUserId(((Repair)object).getUserId());
			repair.setRequestmessage(((Repair)object).getRequestmessage());
			repair.setStates(((Repair)object).getStates());
			session.save(repair);
		}
		else if(tablename.equals("message")){
			Message message=(Message)session.get(Message.class, id);
			Message content = (Message)object;
			message.setMesid(content.getMesid());
			message.setMessage(content.getMessage());
			message.setMestime(content.getMestime());
			session.save(message);
		}
		transaction.commit();
		session.close();
	}

}
